/**
 * 船模 列表页
 *
 * 作者 ganjun
 * 日期 2024-11-13 19:03:49
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'dropdown', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        dropdown = layui.dropdown,
        commonJS = layui.commonJS,
        validate = layui.validate,
        laydate = layui.laydate,
        $view = $('#febs-modelShip'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#modelShip-table-form'),
        $statusSearch = $view.find('#statusSearchSelect'),
        tableIns;

    form.verify(validate);
    form.render();

    //状态
    febs.initDictItemSelect($statusSearch, 'MODEL_SHIP_PROCESS_STATUS', null, null);

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'modelShipTable',
            url: ctx + 'modelShip/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {field: 'projectCode', title: '项目编号', minWidth: 120},
                {field: 'projectName', title: '项目名称', minWidth: 200},
                {field: 'shipCode', title: '船模编号', minWidth: 140},
                {field: 'shipName', title: '名称', minWidth: 120},
                {field: 'sourceName', title: '船模来源', minWidth: 80},
                {field: 'statusName', title: '状态', minWidth: 80},
                {field: 'demandEnddate', title: '要求完成时间', minWidth: 140},
                {field: 'actuallyStartdate', title: '实际开始时间', minWidth: 140},
                {field: 'actuallyEnddate', title: '实际完成时间', minWidth: 140},
                // {title: '操作', toolbar: '#modelShip-tool', width: 540,align:'center',fixed:'right'}
                {title: '操作', width: 230, align: 'left',fixed:'right', templet: function (d) {
                        var str = ' <div class="layui-clear-space">';
                        str += '<a lay-event="edit" shiro:hasPermission="modelShip:edit">\n' +
                            '            <em class="layui-icon febs-edit-area febs-blue">&#xe7a4;</em>编辑</a>';
                        str += '<a lay-event="del" shiro:hasPermission="modelShip:delete">'+
                            '<em class="layui-icon febs-edit-area febs-red">&#xe7f9;</em>删除</a>';
                        str += '<a lay-event="view" shiro:hasPermission="modelShip:view">'+
                            '<em class="layui-icon febs-edit-area febs-cyan">&#xe7b3;</em>详情</a>';
                        /*if(d.source == 1){
                            str += '<a lay-event="process" shiro:hasPermission="modelShip:process">'+
                                '<em class="layui-icon febs-edit-area febs-gold">&#xe7a5;</em>跟踪</a>';
                        }else if((d.source == 2 || d.source == 3) && d.status != 9){
                            str += '<a lay-event="outFinish" shiro:hasPermission="modelShip:outFinish">'+
                                '<em class="layui-icon febs-edit-area febs-green">&#xe77d;</em>完成</a>';
                        }*/
                        str += '<a style="margin-left: 7px;" class="layui-btn layui-btn-xs" lay-event="more">' +
                            '更多<i class="layui-icon layui-icon-down"></i></a></div>';
                        return str;
                    }
                }
            ]],
            toolbar: '#modelShip-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(modelShipTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(modelShipTable)', function (obj) {
        if (obj.event === 'add') {
            /*febs.modal.open('新增', 'modelShip/add', {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? ['90%', '90%'] : ['80%', '90%'],
                yes: function (index, layero) {
                    $('#modelShip-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                }
            });*/
            parent.layui.febs.navigate("/modelShip/add", true);
        }

        //导出列表
        if(obj.event === 'export'){
            var params = getQueryParams();
            febs.download(ctx + 'modelShip/export', params, '船模信息表.xls');
        }

    });

    table.on('tool(modelShipTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.id);
            });
        }
        if (layEvent === 'edit') {
            parent.layui.febs.navigate('/modelShip/update/' + data.id);
            /*febs.modal.open('修改', 'modelShip/update/' + data.id, {
                area: $(window).width() <= 750 ? ['90%', '90%'] : ['80%', '90%'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#modelShip-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });*/
        }

        if (layEvent === 'view') {
            /*febs.modal.open('查看', 'modelShip/view/' + data.id, {
                area: $(window).width() <= 750 ? ['90%', '90%'] : ['80%', '90%'],
                btn: ['取消'],
                btn2: function () {
                    layer.closeAll();
                },
            });*/
            parent.layui.febs.navigate('/modelShip/view/' + data.id);
        }

        /*if (layEvent === 'process') {
            /!*febs.modal.open('加工流程', 'modelShip/process/' + data.id, {
                area: $(window).width() <= 750 ? ['90%', '90%'] : ['70%', '90%'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#modelShip-process').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
            });*!/
            parent.layui.febs.navigate('/modelShip/process/' + data.id);
        }


        if (layEvent === 'outFinish') {
            febs.modal.open('完成', 'modelShip/outFinish/' + data.id, {
                area: $(window).width() <= 750 ? ['40%', '50%'] : ['40%', '50%'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#modelShip-outFinish').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
            });
        }*/

        //更多下拉按钮选项
        if(layEvent === 'more'){
            button = [];

            if(data.source == 1){
                //跟踪
                var processBtn = {
                    id: 'processBtn',
                    title: '跟踪',
                    templet: '<a><em class="layui-icon febs-edit-area febs-gold">&#xe7a5;</em>跟踪</a>'
                };

                if(febs.include(permissionSet,"modelShip:process")){
                    button.push(processBtn);
                }

                //加工申请表
                var expApplyBtn = {
                    id: 'expApplyBtn',
                    title: '加工申请表',
                    templet: '<a><em class="layui-icon febs-edit-area febs-tag-geekblue">&#xe7eb;</em>加工申请表</a>'
                };
                if(febs.include(permissionSet,"modelShip:view")){
                    button.push(expApplyBtn);
                }


                if(data.status == 9){
                    //质量检验表
                    var expCheckBtn = {
                        id: 'expCheckBtn',
                        title: '质量检验表',
                        templet: '<a><em class="layui-icon febs-edit-area febs-tag-geekblue">&#xe7eb;</em>质量检验表</a>'
                    };
                    if(febs.include(permissionSet,"modelShip:view")){
                        button.push(expCheckBtn);
                    }

                    //加工进度及检验表
                    var expProcBtn = {
                        id: 'expProcBtn',
                        title: '加工进度及检验表',
                        templet: '<a><em class="layui-icon febs-edit-area febs-tag-geekblue">&#xe7eb;</em>加工进度及检验表</a>'
                    };
                    if(febs.include(permissionSet,"modelShip:view")){
                        button.push(expProcBtn);
                    }

                }
            }else if(data.source == 2 || data.source == 3){
                if(data.status != 9){
                    var outFinishBtn = {
                        id: 'outFinishBtn',
                        title: '完成',
                        templet: '<a><em class="layui-icon febs-edit-area febs-green">&#xe77d;</em>完成</a>'
                    };
                    if(febs.include(permissionSet,"modelShip:outFinish")){
                        button.push(outFinishBtn);
                    }

                }
            }

            dropdown.render({
                elem: this, // 触发事件的 DOM 对象
                show: true, // 外部事件触发即显示
                data: button,
                click: function (menudata) {
                    if (menudata.id === 'processBtn') {
                        parent.layui.febs.navigate('/modelShip/process/' + data.id);
                    } else if (menudata.id === 'outFinishBtn') {
                        febs.modal.open('完成', 'modelShip/outFinish/' + data.id, {
                            area: $(window).width() <= 750 ? ['40%', '50%'] : ['40%', '50%'],
                            btn: ['提交', '取消'],
                            yes: function (index, layero) {
                                $('#modelShip-outFinish').find('#submit').trigger('click');
                            },
                            btn2: function () {
                                layer.closeAll();
                            },
                        });
                    } else if (menudata.id === 'expApplyBtn') {
                        var param = {};
                        param.id = data.id;
                        febs.download(ctx + 'modelShip/expApplyForm', param, '加工申请表.docx');
                    } else if (menudata.id === 'expCheckBtn') {
                        var param = {};
                        param.id = data.id;
                        febs.download(ctx + 'modelShip/expCheckForm', param, '船模质量检验表.docx');
                    } else if (menudata.id === 'expProcBtn') {
                        var param = {};
                        param.id = data.id;
                        febs.download(ctx + 'modelShip/expProcForm', param, '加工进度及检验表.docx');
                    }
                },
                align: 'right', // 右对齐弹出
                style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
            });
        }

        /*if(layEvent === 'expApply'){
            var param = {};
            param.id = data.id;
            febs.download(ctx + 'modelShip/expApplyForm', param, '加工申请表.pdf');
        }

        if(layEvent === 'expCheck'){
            var param = {};
            param.id = data.id;
            // febs.download(ctx + 'modelShip/expCheckForm', param, '船模质量检验表.docx');
            febs.download(ctx + 'modelShip/expCheckForm', param, '质量检验表.pdf');
        }

        if(layEvent === 'expProc'){
            var param = {};
            param.id = data.id;
            febs.download(ctx + 'modelShip/expProcForm', param, '加工进度及检验表.pdf');
        }*/
    });

    // $delete.on('click', function () {
    //     var checkStatus = table.checkStatus('modelShipTable');
    //     if (!checkStatus.data.length) {
    //         febs.alert.warn('没有选择要删除的数据！');
    //     } else {
    //         febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
    //             var ids = [];
    //             layui.each(checkStatus.data, function (key, item) {
    //                 ids.push(item.id);
    //             });
    //             deleteIds(ids.join(','));
    //         });
    //     }
    // });

    function getQueryParams() {
        return {
            name: $searchForm.find('input[name="name"]').val().trim(),
            demondFinishTime: $searchForm.find('input[name="demondFinishTime"]').val().trim(),
            statusSearch: $searchForm.find("select[name='statusSearch']").find("option:selected").val(),
            shipCodeSearch: $searchForm.find('input[name="shipCodeSearch"]').val().trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'modelShip/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });


    laydate.render({
        elem: "#demondFinishTime",
        type: 'datetime',
        range: true,
        format: 'yyyy-MM-dd HH:mm',
    });

});